<template>
  <div class="container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="候选人" name="first">
        <div class="title">
          {{ title }}<span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
        </div>
        <div v-if="form" class="wrap" >
          <up-load></up-load>
          <el-row>
            <el-col :lg="16" :md="20" :sm="24" :xs="24" :span="8">
              <el-form :model="form" status-icon ref="form" label-width="100px" @submit.prevent  :rules="rules">
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="姓名" prop="candName">
                      <el-input v-model="form.candName" :disabled="formVal1" placeholder="请填写姓名"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="电话" prop="phone">
                      <el-input v-model="form.phone" :disabled="formVal2" placeholder="请填写电话"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="所属HR" prop="belongUserId">
                      <el-select
                        :disabled="formVal3"
                        style="width: 100%"
                        size="medium"
                        clearable
                        filterable
                        v-model="form.belongUserId"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in getDictiteListHr"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item  v-if="!right1" label="推荐日期" prop="recDate">
                      <el-date-picker
                        :disabled="formVal4"
                        :disabledDate="false"
                        v-model="form.recDate"
                        type="date"
                        placeholder="选择日期"
                        :picker-options="pickerOptions"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                </el-row>
                <!-- <el-form-item label="所属HR" >
                  <el-input v-model="candidate.belongUserName" placeholder="请填写所属HR"></el-input>
                </el-form-item>
                <el-form-item label="推荐日期">
                  <el-input v-model="candidate.recDate" placeholder="请填写推荐日期"></el-input>
                </el-form-item> -->
                <!-- <el-form-item label="所属部门" >
                  <el-input v-model="candidate.jobInfo.deptName" placeholder="请填写所属部门"></el-input>
                </el-form-item> -->
                <el-row>
                  <!-- <el-col :span="12">
                    <el-form-item label="所属部门" prop="deptName">
                      <el-select filterable style="width: 100%" v-model="form.deptId" placeholder="请填写职位名称">
                        <el-option
                          v-for="(item, index) in getDictiteList10"
                          :key="index"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col> -->
                  <el-col :span="12">
                    <el-form-item label="推荐职位" prop="jobId">
                         <div class="select-box">
                            <el-cascader :disabled="formVal5" style="width: 100%" v-model="jobIdArr" clearable @change="handkeChange" :options="getDictiteList11"></el-cascader>
                          </div>
                    </el-form-item>
                  </el-col>
                </el-row>

                <!-- <el-row>
                 <el-col :span="12"></el-col>
                 <el-col :span="12"></el-col>
               </el-row> -->
                <el-row>
                  <el-col :span="12">
                    <el-form-item v-if="!right1" label="候选人状态">
                      <el-select
                      :disabled="formVal6"
                        style="width: 100%"
                        size="medium"
                        filterable
                        v-model="form.candStatu"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in getDictiteList2"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="邮箱">
                      <el-input :disabled="formVal7" v-model="form.email" placeholder="请填写邮箱"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <!-- <el-form-item label="评价" >
                  <el-input
                    type="textarea"
                    :autosize="{ minRows: 4, maxRows: 8 }"
                    placeholder="请输入简介"
                    v-model="form.summary"
                  >
                  </el-input>
                </el-form-item> -->
                <el-form-item class="submit">
                  <el-button v-if="right1" type="success" @click="check">排查重复候选人</el-button>
                  <el-button v-if="right2" type="primary" @click="submitForm">保 存</el-button>
                  <el-button v-if="right3" type="primary" @click="submitForm">确 定</el-button>
                  <el-button v-if="right7" type="primary" @click="submitForm">确 定</el-button>
                  <el-button v-if="right4" type="danger" @click="submitForm">淘 汰</el-button>
                  <el-button v-if="right5" type="danger" @click="submitForm">删 除</el-button>
                  <el-button v-if="right6" @click="resetForm">重 置</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
      </el-tab-pane>
      <el-tab-pane label="操作日志" name="second">
        <div class="title">操作日志</div>
        <div style="margin: 20px 100px">
          <el-steps direction="vertical" :active="19">
            <el-step style="height: 100px" title="步骤 1" description="这是一段很长很长很长的描述性文字"></el-step>
            <el-step style="height: 100px" title="步骤 2" description="这是一段很长很长很长的描述性文字"></el-step>
            <el-step style="height: 100px" title="步骤 3" description="这是一段很长很长很长的描述性文字"></el-step>
            <el-step style="height: 100px" title="步骤 4" description="这是一段很长很长很长的描述性文字"></el-step>
          </el-steps>
        </div>
      </el-tab-pane>
    </el-tabs>
      <el-dialog title="提示" :visible.sync="showDialog" center>
      <div class="dialog-box">
        <div class="dialog-title">确认要{{showTitle}}吗？</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="() => {(showDialog = false), (delId = '')}"
          >取 消</el-button
        >
        <el-button type="primary" @click="handleDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, onMounted } from 'vue'
import Vue from 'vue'
// import { ElMessage } from 'element-plus'
import candidateModel from '@/model/candidate'
import toolModel from '@/model/tool'
import upLoad from './upload'
export default {
  props: {
    editData: {
      type: Object,
      default: null,
    },
    title: {
      type: String,
      default: '',
    },
    getDictiteListHr: {
      type: Array,
      default: [],
    },
    getDictiteList11: {
      type: Array,
      default: [],
    },
  },
  components: {
    upLoad,
  },
  data() {
    return {
      jobIdArr:[],
      showDialog:false,
      showTitle:"",
      right1:false,
      right2:false,
      right3:false,
      right4:false,
      right5:false,
      right6:false,
      right7:false,
      formVal1:false,
      formVal2:false,
      formVal3:false,
      formVal4:false,
      formVal5:false,
      formVal6:false,
      formVal7:false,
      form: {
        candName: '',
        phone: '',
        belongUserName: '',
        belongUserId: '',
        recDate: '',
        deptName: '',
        deptId: '',
        jobId: '',
        jobName: '',
        candStatu: '',
        candStatuLabel: '',
        email: '',
        summary: '',
      },
      //时间选择参数
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
      },
      getDictiteList1: [],
      getDictiteList2: [],
      getDictiteList3: [],
      getDictiteList4: [],
      getDictiteList5: [],
      getDictiteList6: [],
      getDictiteList7: [],
      getDictiteList8: [],
      getDictiteList9: [],
      getDictiteList10: [],
      // getDictiteList11: [],
      detailName: '',
      activeName: 'first',
      loading: false,
      candidate: {},
      rules: {
        candName: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确的手机号码', trigger: 'blur' },
        ],
        belongUserId: [
          { required: true, message: '请输入所属HR', trigger: 'blur' },
        ],
        jobId: [
          { required: true, message: '请输入推荐日期', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    handkeChange(){
    },
    listAssign(a, b) {
      Object.keys(a).forEach(key => {
        a[key] = b[key] || a[key]
      })
    },
    async checkEmpty(){
        var  flag = true
       this.$refs.form.validate((valid) => {
         console.log(valid,'valids');
          if (valid) {
            flag = true
          } else {
                Vue.prototype.$notify({
                title: "提示",
                dangerouslyUseHTMLString: false,
                message: '请按要求输入',
                })
            flag = false
          }
        });
        return flag
    },
    async check(){
      var that = this
      if(!that.form.candName||!that.form.phone){
          Vue.prototype.$notify({
        title: "提示",
        dangerouslyUseHTMLString: false,
        message: '请输入姓名和电话',
        })
        return
      }
      let data = {
        candName:that.form.candName,
        phone:that.form.phone,
      }
      let res = await candidateModel.checkDuplicat(data)
      if(res.code = 200){
        if(res.data.suspecCount>0) {
          Vue.prototype.$notify({
        title: "提示",
        dangerouslyUseHTMLString: false,
        message: '候选人信息已存在',
        })
      }else{
           Vue.prototype.$notify({
        title: "提示",
        dangerouslyUseHTMLString: false,
        message: '候选人信息可用',
        })
      }
      }
    },
    // 重置表单
    resetForm() {
      // this.form.resetFields()
      this.init()
    },
    async handleDialog(){
      var that = this
      that.showDialog = true
      let behavior = that.editData.behavior
       switch(behavior){
        case 'add':
        that.candidateAdd()
        // that.showTitle = "新增候选人"
        break
        case "edit":
        that.candidateModify()
        // that.showTitle = "保存编辑候选人"
        case "distribution":
        this.candidateModify()
          //分配职位
        that
        break
        default :
        break
      }
    },
    async submitForm(){
      var that = this
      let flag = await that.checkEmpty()
      if(!flag) return
      that.showDialog = true
      let behavior = that.editData.behavior
      switch(behavior){
        case 'add':
        // that.candidateAdd()
        that.showTitle = "新增候选人"
        break
        case "edit":
        // that.candidateModify()
        that.showTitle = "保存编辑候选人"
        break
        case 'distribution':
        that.showTitle = "分配职业"
        break
        default :
        break
      }
    },
    back() {
      this.$emit('editClose')
    },
    handleClick() {},
    async candidateAdd() {
      var that = this
      const data = {
        //候选人姓名
        candName: that.form.candName,
        //手机号
        phone: that.form.phone,
        //邮箱
        email: that.form.email,
        //所属hr id
        // belongUserId: that.form.belongUserId,
        operateUserId:that.form.belongUserId,
        //推荐日期
        recDate: that.form.recDate,
        // 所属部门id
        deptId: that.jobIdArr[0],
        
        //选择职位
        jobId: that.jobIdArr[1],
        // 候选人状态
        candStatu: that.form.candStatu,
        uploadPath: 'http://192.168.1.9:8282/web2/candidate/downloadFile?filePath=简历.pdf',
      }
      console.log(this.getDictiteList10);
      console.log(data,'data');
    const res = await candidateModel.candidateAdd(data)
      if(res.code == 200){
        Vue.prototype.$notify({
        title: res.msg,
        dangerouslyUseHTMLString: false,
        message: res.data,
      })
      }else{
        Vue.prototype.$notify({
        title: res.msg,
        dangerouslyUseHTMLString: false,
        message: res.data,
      })
      }
      that.back()
    },
    async candidateModify() {
      var that = this
      const data = {
        candId: that.form.candId,
        candName: that.form.candName,
        phone: that.form.phone,
        email: that.form.email,
        operateUserId:that.form.belongUserId,
        deptId: that.jobIdArr[0],
        jobId: that.jobIdArr[1],
        candStatu: that.form.candStatu,
        uploadPath: 'http://192.168.1.9:8282/web2/candidate/downloadFile?filePath=简历.pdf',
      }
      const res = await candidateModel.candidateModify(data)
      if(res.code == 200){
        Vue.prototype.$notify({
        title: res.msg,
        dangerouslyUseHTMLString: false,
        message: res.data,
      })
      }else{
          Vue.prototype.$notify({
        title: res.msg,
        dangerouslyUseHTMLString: false,
        message: res.data,
      })
      }
      that.back()
    },
    async getDictiteList(dictId) {
      var that = this
      const res = await toolModel.dictitem({
        dictId: dictId,
      })
      const allData = res.data
      const result = []
      allData.map(ele => {
        result.push({
          value: Number(ele.itemValue),
          label: ele.itemText,
        })
      })
      console.log(result, '字典', dictId)
      return result
    },
    async candidateReallocate(){
      var that = this
      let param = {
        candId:that.form.candId,
        // deptId: that.jobIdArr[0],
        jobId: that.jobIdArr[1],
      }
      let res = candidateModel.candidateReallocate(param)
      if(res.code = 200){
      }
    },
    right(behavior){
      var that = this
      switch(behavior){
        case 'add':
          that.right1 = true
          that.right3 = true
          that.right6 = true
          break
        case 'edit':
          that.right2 = true
          break
        case 'distribution':
          that.right7 = true
          that.formVal1=true
          that.formVal2=true
          that.formVal3=true
          that.formVal4=true
          that.formVal5=false
          that.formVal6=true
          that.formVal7=true
          default:
          break
      }
    },
    async processing(){
      // 数据处理
      var that = this
      var arr = []
      if (that.editData.row) {
        that.loading = true
        arr = JSON.parse(JSON.stringify(that.editData.row))
        //候选人状态
        if (arr.candStatu == 0 || arr.candStatu) {
          arr.candStatuLabel = that.getDictiteList2[arr.candStatu].label
        }
        //职位信息
        if (arr.jobInfo) {
          //职位
          arr.jobId =Number(arr.jobInfo.jobId) 
          arr.deptId = Number(arr.jobInfo.deptId)
          that.jobIdArr = [arr['deptId'], arr['jobId']]
          // arr.deptName = that.getDictiteList10[arr.deptId].label
        }
        // if(arr.deptId==0||arr.deptId){
        //   //部门
        //    arr.deptName = that.getDictiteList10[arr.deptId].label
        // }
        //所属hr
        if (arr.belongUserId == 0 || arr.belongUserId) {
          // arr.belongUserId = that.getDictiteListHr[arr.belongUserId].value
          arr.belongUserId = arr.belongUserId+''
          // arr.belongUserName = that.getDictiteListHr[arr.belongUserId].label
        }
        console.log(arr,this.getDictiteList11,"详情数据");
        that.form = arr
        that.loading = false
      }
    },
    async init() {
      var that = this
      let aaa=  that.listAssign([{r:1},{t:312},{q:'qwe'}], ["1", "2", "3"])
      console.log(Object.keys({1:2,2:1,3:1,}));
      console.log(aaa,'aaa');
      this.getDictiteList2 = await this.getDictiteList(2)
      this.getDictiteList10 = await this.getDictiteList(10)
      //权限处理
      if(that.editData.behavior){
        that.right(that.editData.behavior)
      }
      this.processing()
      
    },
     getDate(date) {
      let myDate = new Date(date)
      let y = myDate.getFullYear()
      let m = myDate.getMonth() + 1
      let d = myDate.getDate()
      if (m < 10) {
        m = '0' + m
      }
      if (d < 10) {
        d = '0' + d
      }
      return '' + y + '-' + m + '-' + d
    },
  },
  mounted() {
    this.init()
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;
 .dialog-box {
    text-align: center;
    padding: 0 80px;
  }

  .dialog-title {
    line-height: 24px;
    font-size: 16px;
    margin-bottom: 10px;
    // color: #f4516c;
  }
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
